
/* Add onload event for the top right eip status */
// addLoadEvent(updateStatusBug);		
// IP Address capture
var ip_address;
function alertIP(ipval) {
// alert(">> ipval: " + ipval);
ip_field = document.getElementById('ip_address');
if (ip_field != null)
{
ip_field.value = ipval;
}
}			
function thisMovie(movieName) {
if (navigator.appName.indexOf("Microsoft") != -1) {
return window[movieName];
} else {
return document[movieName];
}
}
function sendToActionScript() {
ip_field = document.getElementById('ip_address').value;
if (ip_field == "")
{
thisMovie("iptracker").getUserIP();
}
}
function textCounter(field,cntfield,maxlimit) 
{
var maxChar = "";
if (field.value.length > maxlimit) 
{ // if too long...trim it!
field.value = field.value.substring(0, (maxlimit));
if (cntfield != "")
{
document.getElementById(cntfield).innerHTML = "Characters Remaining - 0";
} else {
alert("Characters Remaining - 0");
}
} else {
if (cntfield != "")
{
document.getElementById(cntfield).innerHTML = "Characters Remaining - " + (maxlimit - field.value.length);
}
}
}
function decodeHex(str)
{
// Strip out non alpha/num chars
str = str.toUpperCase().replace(new RegExp("s/[^0-9A-Z]//g"));
var result = "";
var nextchar = "";
// loop through the input string
for (var i=0; i<str.length; i++)
{
nextchar += str.charAt(i);
// every 2nd char, add a % and unescape
if (nextchar.length == 2)
{
result += unescape("%"+nextchar);
nextchar = "";
}
}
return result;
}
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1)
{ 
c_start=c_start + c_name.length+1; 
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
} 
}
return "";
}
function deleteCookie(c_name, path, domain) 
{
if (getCookie(c_name))
{
document.cookie = c_name + "=" + 
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
}
var firstsubmit = true;
function submitForm(form,divid)
{
var postURL = "";
var postQuery = "";
var emailQuery = "";
var subscribe = false;
var measureId = "comment_submit";
if (!firstsubmit)
{
return;
}
// check for a comment
if (divid == "commentform")
{
if (!commentValidate(form))
{
return;
}
}
// validated, set submit flag to false
firstsubmit = false;
// get the action from the form
postURL += form.action;
// Loop through the form elements and add them to the query str
for (i =0; i < form.elements.length; i++)
{
// add validation
// skip the sd field since we are doing ajax
if (form.elements[i].name != "sd")
{
if (form.elements[i].type == "radio")
{
if(form.elements[i].checked)
{
// first item starts the query str
postQuery += (i == 0)?"":"&";
postQuery += encodeURIComponent(form.elements[i].name) + "=" + encodeURIComponent(form.elements[i].value);						
}
} else if (form.elements[i].type == "checkbox")
{
if(form.elements[i].name == "news" && form.elements[i].checked)
{
subscribe = true;
}
} else {
var elementValue = form.elements[i].value;
// combine the ip and email
if (form.elements[i].name == "emailAddress")
{
// add the value to the newsletter queryStr
emailQuery += (emailQuery.length == 0)?"":"&";
emailQuery += "Email="+encodeURIComponent(elementValue);
// get the ip
var ipaddr = document.getElementById('ip_address');
if (ipaddr != null)
{
// prepend it to the email addr
elementValue = ipaddr.value + "|" + elementValue;
}
}
if (form.elements[i].name == "firstName")
{
emailQuery += (emailQuery.length == 0)?"":"&";
emailQuery += "FirstName="+encodeURIComponent(elementValue);
}
if (form.elements[i].name == "lastName")
{
emailQuery += (emailQuery.length == 0)?"":"&";
emailQuery += "LastName="+encodeURIComponent(elementValue);
}
// update the submission info
// c
if (form.elements[i].name == "c")
{
elementValue = pageId;
}
// subject
if (form.elements[i].name == "subject")
{
elementValue = pageTitle;
}
// first item starts the query str
postQuery += (i == 0)?"":"&";
postQuery += encodeURIComponent(form.elements[i].name) + "=" + encodeURIComponent(elementValue);
}
}
if (form.elements[i].name == "type")
{
measureId = form.elements[i].value;
}
}
var responseAction = "";
// set this as the div on display
activeDiv = divid;
// set the responseAction function
switch (divid)
{
case "commentbox":
responseAction = onCommentResponse;
break;
case "commentform":
responseAction = onCommentResponse;
break;
default:
responseAction = onCommentResponse;
}				
// make a measurement call
measureComment(measureId);
if(!subscribe)
{
emailQuery = "";
}
/* if (window.console && window.console.error) {
console.error(postURL+"?"+postQuery);
} */
// call the ajax function
$.ajax({
url: postURL,
data: postQuery,
type: 'POST',
dataType: 'xml',
timeout: 15000,
error: function(){
// alert('An error has occured, please reload the page.');
},
success: function(xml){
responseAction(xml,emailQuery);
}
});
}
function onCommentResponse(xmlDoc,email) {
//if(loginAjax.checkReadyState() == "success")
//{
// var xmlDoc = loginAjax.request.responseXML;
// get status
var statusNodes = xmlDoc.getElementsByTagName("status");
var status = statusNodes[0].childNodes[0].nodeValue;
// get the div info
var msgDiv = document.getElementById('msgbox');
var msgTxt = "";
// if response is not successful show errors
if (status == "Success")
{
msgTxt = 	"<b>Thank You for submitting a comment. Your comment will be reviewed and will appear on "+
"the site if possible. Due to the large volume of submissions all comments cannot appear on "+
"the site.</b><br/><a href='javascript:void(0)' class='blue2' onClick='document.getElementById(\"postmsg\").style.display=\"none\";'><b>Submit another comment?</b></a>";
// clear the form
var thisForm = document.getElementById('comment_form');
if (thisForm != null)
{
thisForm.reset();
}
// set firstsubmit flag to true
firstsubmit = true;
// if they want to subscribe to the email call the email function
if (email != null && email != "")
{
emailSubscribe(email);
}
} else {
// default message
msgTxt = "An error has occurred. Please try to submit your comment again.";
// get the error message
var errorNodes = xmlDoc.getElementsByTagName("error");
var errorTxt = errorNodes[0].childNodes[0].nodeValue;
if (errorTxt != null && errorTxt != "")
{
if (errorTxt == "User is not logged in")
{
// 
msgTxt = "<p><b>Please <a href='javascript:void(0)' onclick='popOver(\"login\");'>sign in</a> to submit your comment</b></p>";
} else {
msgTxt = "<p><b>"+errorTxt+"</b></p>";
}
}
}
msgDiv.innerHTML = msgTxt;
if (document.getElementById('postmsg') != null)
{
document.getElementById('postmsg').style.display = 'block';
}				
//}
}
function emailSubscribe(email)
{
if (email != null && email != "")
{
/* if (window.console && window.console.error) {
console.error("http://assets.tennis.com.au/email_newsletter/subscribe.asp?"+email);
} */
var emailFrm = document.createElement('iframe');
emailFrm.style.display = 'none';
emailFrm.src = 'http://assets.tennis.com.au/email_newsletter/subscribe.asp?'+email;
document.body.appendChild(emailFrm);
return;
}			
}
/* Comments Functions */
function checkUser()
{
// check for the session cookie 
userCookie = getCookie("eipatron");
// alert(userCookie);
var commentBox = document.getElementById('commentform');
var signinBox = document.getElementById('signinbox');
// if cookie's empty, etc make them sign in
if (userCookie == null || userCookie == "")
{
if (signinBox != null)
{
signinBox.style.display = 'block';
}
if (commentBox != null)
{
commentBox.style.display = 'none';
}
} else {
if (signinBox != null)
{
signinBox.style.display = 'none';
}
if (commentBox != null)
{
commentBox.style.display = 'block';
}				
}
}
function convertMS(responseTxt)
{
// alert(responseTxt);
// Mozilla and Netscape browsers
if (document.implementation.createDocument) {
var parser = new DOMParser()
doc = parser.parseFromString(responseTxt, "text/xml")
// MSIE
} else if (window.ActiveXObject) {
doc = new ActiveXObject("Microsoft.XMLDOM")
doc.async="false"
doc.loadXML(responseTxt)
}
return doc;
}
// var commentAjax = new Ajax();
function getComments(xmlDoc)
{
var commentHTML = "";
//if(commentAjax.checkReadyState() == "success") {
// var xmlDoc = commentAjax.request.responseXML;
// get status
var statusNodes = xmlDoc.getElementsByTagName("status");
var status = statusNodes[0].childNodes[0].nodeValue;
// if status = success
if (status == "Success")
{					
// set the count
var countNode = xmlDoc.getElementsByTagName("count");
var count = 0;
if (countNode.length > 0)
{
count = countNode[0].childNodes[0].nodeValue;
}
if (count > 0)
{
// Set the comment count
commentCount(count);
}
// get comment node
var commentNodes = xmlDoc.getElementsByTagName("comment");
// loop through and set the comments div comments
for (i=0;i<commentNodes.length;i++) {
commentData = commentNodes.item(i).childNodes;
commentCell = '';
date = '';
name = '';
state = '';
country = '';
subject = '';
body = '';
for (var j=0; j<commentData.length; j++){
switch(commentData.item(j).nodeName){
case "name": name = commentData.item(j).hasChildNodes() ? commentData.item(j).firstChild.nodeValue : ""; break;
case "subject": subject = commentData.item(j).hasChildNodes() ? commentData.item(j).firstChild.nodeValue : ""; break;
case "country": country = commentData.item(j).hasChildNodes() ? commentData.item(j).firstChild.nodeValue : ""; break;
case "date": date = commentData.item(j).hasChildNodes() ? commentData.item(j).firstChild.nodeValue : ""; break;
// case "state": state = commentData.item(j).hasChildNodes() ? commentData.item(j).firstChild.nodeValue : ""; break;
case "body": body = commentData.item(j).hasChildNodes() ? commentData.item(j).firstChild.nodeValue : ""; break;
default:break;
}
}
commentHTML += "<div class='aihComment'>";
//if (subject != "")
//	commentHTML += "<b>Re:" + subject + "</b><br/>";
if (name != "")
{
commentHTML += "<div id='fanInfoATE'>" + name;
//if (state != "")
//	commentHTML += ", " + state;
if (country != "")
commentHTML += ", " + country;
commentHTML += "</div>";
}
if (body != "")
commentHTML += body + "<br/>";
if (date != "")
{
// create a date object
var d = new Date();
// parse the xml date
d.setTime(date);
// format the date by users locale
commentHTML += d.toLocaleString();
}
commentHTML += "</div>";
}
if (count > 0)
{
commentHTML += "<p class='newsBackToTop' align='right'><a href='#'>Back to Top</a></p>";
}
// alert(commentHTML);
commentsDiv = document.getElementById('comments');
commentsDiv.innerHTML = commentHTML;
}
//}
}
function commentCount(n)
{
var commentCount = document.getElementById('commentsCount');
if (commentCount != null)
{
commentCount.innerHTML = '<img style="display:inline; margin-right:10px;" src="/images/misc/aus_ms_00000gy.gif" width="20" height="20" border="0" alt="Read Comments" align="middle"/>' + n + ' comments posted</a>';
}
}
function eipValidation(divid)
{
var isValid = true;
// get the container
var eipContain = document.getElementById('eipcontainer');
var eipForms = null;
// get the form
if (eipContain != null)
{
eipForms = eipContain.getElementsByTagName('form');
}
for (i =0; i < eipForms.length; i++)
{
// switch to diff function based on divid, passing the form
switch(divid)
{
case "register":
isValid = registerValidate(eipForms[i]);
break;
case "signin":
isValid = signinValidate(eipForms[i]);
break;
case "forgotpw":
isValid = forgotpwValidate(eipForms[i]);
break;
case "profile":
isValid = profileValidate(eipForms[i]);
break;
case "revalidate":
isValid = revalidateValidate(eipForms[i]);
break;
default:
isValid = signinValidate(eipForms[i]);
}
}
// return var
// get the general error div
var gen_error_div = document.getElementById('gen_error');
if (gen_error_div != null)
{
if (!isValid)
{
// set the general error
gen_error_div.innerHTML = "We're sorry. We encountered some errors in the form. "+
"Please review and correct the errors as marked in the "+
"highlighted fields below.";
// gen_error_div.className = "p_error";
gen_error_div.style.display = "block";
// resize the iframe
} else {
gen_error_div.innerHTML = "";
gen_error_div.style.display = "none";
}
}
parent.resizeFrame(getDivSize());
return isValid;
}
function setError(eName,msg)
{
var msgDiv = document.getElementById('commentmessage');
//alert(msgDiv);
if (msgDiv != null)
{
msgDiv.innerHTML = "<div class='d_error' style='display:block;'>" + msg + "</div>";
} else {
alert(msg);
}
}
function clearError(eName)
{
// on error set e_<form.element.name>.innerhtml to errormsg
var errorDiv = document.getElementById('e_'+ eName);
if (errorDiv != null)
{
errorDiv.innerHTML = "";
errorDiv.style.display = "none";
}
// 	set p_<form.element.name>.class to p_error
var errorP = document.getElementById('p_'+ eName);
if (errorP != null)
errorP.className = "";
var errorF = document.getElementById(eName);
if (errorF != null)
{
errorF.style.background = "#FFFFFF";
}
}
function commentValidate(form)
{
var regValid = true;
// loop through form elements
for (j =0; j < form.elements.length; j++)
{
// check f name
if (form.elements[j].name == "firstName")
{
if (form.elements[j].value == "")
{
setError(form.elements[j].name,"Please enter your name.");
regValid = false;
return regValid;
}
else
{
// clean the entry
form.elements[j].value = scrubText(form.elements[j].value);
} 
}
// check l name
if (form.elements[j].name == "lastName")
{
if (form.elements[j].value == "")
{
setError(form.elements[j].name,"Please enter your last name.");
regValid = false;
return regValid;
}
}
// check email
if (form.elements[j].name == "emailAddress")
{
if (form.elements[j].value != "")
{
// check email format
var errorTxt = emailCheck2(form.elements[j].value);
if (errorTxt != "")
{
setError(form.elements[j].name, errorTxt);
// form.elements[j].value = "";
regValid = false;
return regValid;
}
}
else
{
setError(form.elements[j].name,"Please enter your Email address.");
regValid = false;
return regValid;
}
}
// check country
if (form.elements[j].name == "country")
{
if (form.elements[j].selectedIndex == 0)
{
setError(form.elements[j].name,"Please choose a country.");
regValid = false;
return regValid;
}
}
// check comment
if (form.elements[j].name == "comment")
{
if (form.elements[j].value == "")
{
setError(form.elements[j].name,"Please enter a comment.");
regValid = false;
return regValid;
}	
else
{
form.elements[j].value = scrubText(form.elements[j].value);
}	
}
// check t's and c's
if (form.elements[j].name == "terms")
{
if (form.elements[j].type == "checkbox" && !form.elements[j].checked)
{
setError(form.elements[j].name,"Please accept the Terms and Conditions.");
regValid = false;
return regValid;
}	
}
}
return regValid;
}
function countryLookup(ccode)
{
countryName = ccode;
if (myCountries != null)
{
for (i=0; i < myCountries.countries.length; i++)
{
if (myCountries.countries[i].ctrycode == ccode)
{
countryName = myCountries.countries[i].ctryname;
}
}
}
return countryName;
}
function scrubText(instr)
{
var newstr = "";
for (i =0; i < instr.length; i++)
{
if (instr.charCodeAt(i) > 30 && instr.charCodeAt(i) < 256)
{
newstr += instr.charAt(i);
}
else 
{
newstr += "_";
}
}
return newstr;
}
function charAlert(instr)
{
var allChars = document.getElementById('charDiv');
allChars.innerHTML = "";
for (i = instr; i > 0; i--)
{
allChars.innerHTML += String.fromCharCode(i) + "<br/>";
}
}
function emailCheck2 (emailStr) 
{
var errMsg = "";
var emailPat=/^(.+)@(.+)$/;
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
var matchArray=emailStr.match(emailPat);
if (matchArray==null) 
{
errMsg = "Email address seems incorrect (check @ and .'s)";
return errMsg;
}
var user=matchArray[1];
var domain=matchArray[2];
if (user.match(userPat)==null) 
{
errMsg = "The username doesn't seem to be valid.";
return errMsg;
}
var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) 
{
for (var i=1;i<=4;i++) 
{
if (IPArray[i]>255) 
{
errMsg = "Destination IP address is invalid!";
return errMsg;
}
}
return errMsg;
}
var domainArray=domain.match(domainPat)
if (domainArray==null) {
errMsg = "The domain name doesn't seem to be valid.";
return errMsg;
}
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) 
{
errMsg = "The address must end with a domain, or two letter country code.";
return errMsg;
}
if (len<2) 
{
errMsg = "This address is missing a hostname!";
return errMsg;
}
return errMsg;
}		
function measureComment(id) {
var url = document.location.href + "_" + id;
//alert(url);
if (window.sa_onclick){
sa_onclick(url);
}
}
