That is some ugly lookin' code. I am by no means a Javascript man, but try this. Write out the Javascript function (complete with PHP variables) and resultant HTML as though it were not being echoed by PHP, complete with line breaks, indentation, and without escaping double quotes, etc. I think it would look something like this:
Then we can use the extremely useful HEREDOC syntax in PHP to echo this all as typed and with variable substitution without having to escape all those pesky quotes and whatnot, viz:
If it still doesn't work, you know it is the Javascript function that is the problem and not the PHP.