// General Functions

function pI() {var i,d=document,r=pI.arguments; if(d.images) {if(!d.AC_p) d.AC_p=new Array; var j=d.AC_p.length; for(i=0;i<r.length;i++) if(r[i].indexOf("#")!=0) {d.AC_p[j]=new Image; d.AC_p[j++].src=r[i]}}}
function sI() {var i,j=0,o,d=document,r=sI.arguments; d.AC_a=new Array; for(i=0;i<(r.length-1);i+=2) {o=eval("d."+r[i]); if(o) {d.AC_a[j++]=o; if(!o.oSrc) o.oSrc=o.src; o.src=r[i+1]}}}
function sIR() {var i,o,a=document.AC_a; for(i=0;a&&i<a.length;i++) {o=a[i]; o.src=o.oSrc}}


// Popup Windows

var ChatWin
function popChatWin(i) {
var winX,winY,w=620,h=480
winX=(screen.availWidth-w)/2; winY=(screen.availHeight-h)/2
TnCWin=window.open(i,"TnC","width="+w+",height="+h+",left="+winX+",top="+winY+",scrollbars"); TnCWin.window.focus()
}

var TnCWin
function popTnCWin(i) {
var winX,winY,w=540,h=400
winX=(screen.availWidth-w)/2; winY=(screen.availHeight-h)/2
TnCWin=window.open(i,"TnC","width="+w+",height="+h+",left="+winX+",top="+winY+",scrollbars"); TnCWin.window.focus()
}

var vpWin
function popVPWin(i) {
var winX,winY,w=250,h=230
winX=(screen.availWidth-w)/2; winY=(screen.availHeight-h)/2
vpWin=window.open(i,"VP","width="+w+",height="+h+",left="+winX+",top="+winY+",scrollbars"); vpWin.window.focus()
}

var mmsImgWin
function popMMSImg(i) {
var winX,winY,w=240,h=200
winX=(screen.availWidth-w)/2; winY=(screen.availHeight-h)/2
mmsImgWin=window.open(i,"mmsImg","width="+w+",height="+h+",left="+winX+",top="+winY); mmsImgWin.window.focus()
}

var mmsViewWin
function popMMSView(i) {
var winX,winY,w=350,h=400
winX=(screen.availWidth-w)/2; winY=(screen.availHeight-h)/2
mmsViewWin=window.open(i,"mmsView","width="+w+",height="+h+",left="+winX+",top="+winY+",scrollbars"); mmsViewWin.window.focus()
}

var ptCtyWin
function popPTCty(g) {
var winX,winY,w=430,h=450
winX=(screen.availWidth-w)/2; winY=(screen.availHeight-h)/2
if(g.options[g.selectedIndex].value) {
	ptCtyWin=window.open(g.options[g.selectedIndex].value,"ptCty","width="+w+",height="+h+",left="+winX+",top="+winY+",scrollbars"); ptCtyWin.window.focus()
	g.selectedIndex = g.defaultSelected
}}
function popPTCtyLink(i) {
var winX,winY,w=430,h=450
winX=(screen.availWidth-w)/2; winY=(screen.availHeight-h)/2
ptCtyWin=window.open(i,"ptCty","width="+w+",height="+h+",left="+winX+",top="+winY+",scrollbars"); ptCtyWin.window.focus()
}

var MPGWin
function popMPGWin(i) {
var winX,winY,w=500,h=500
winX=(screen.availWidth-w)/2; winY=(screen.availHeight-h)/2
MPGWin=window.open(i,"MPG","width="+w+",height="+h+",left="+winX+",top="+winY+""); MPGWin.window.focus()
}


// Form Functions

function writeDateList(dd){
for(i=1; i<=31; i++){
	if(i==parseInt(dd,10)) {document.write("<option value="+insZero(i,2)+" selected>"+insZero(i,2))}
	else {document.write("<option value="+insZero(i,2)+">"+insZero(i,2))}
}}

smonths = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')
function writeMonthList(mm){
for(i=1; i<=12; i++){
	if(i==parseInt(mm,10)) {document.write("<option value="+insZero(i,2)+" selected>"+smonths[i-1])}
	else {document.write("<option value="+insZero(i,2)+">"+smonths[i-1])}
}}

function insZero(num,sf){
var inputStr=num.toString()
while(inputStr.length<sf) {inputStr="0"+inputStr}
return inputStr
}

function isBlank(input){
if(!input.value) {return true}
var inputStr=input.value
if(window.RegExp && inputStr!=""){
	var regExp1 = /\S/
	if(!regExp1.test(inputStr)) {input.value="";return true}; return false
}}

function keywordSearch(f){
if(isBlank(f.keywords)||f.keywords.value==f.keywords.defaultValue) {alert("Please enter the Keyword that you would like to search for."); f.keywords.focus(); return false}
return true
}

function memberLogin(f){
if(isBlank(f.Mobile) || !f.Mobile.value || !f.Password.value) {alert("Please enter both your Mobile No. and Password to login."); return false}
return true
}

function isModified(input) {if(input.style) input.style.background=(input.value==input.defaultValue)?"#FFFFFF":"#E8CFC4"}

function goURL(g) {if(g.options[g.selectedIndex].value) {document.location=g.options[g.selectedIndex].value}}

function mobileCheck(f)
{
	if(isBlank(f.Mobile) || !f.Mobile.value) 
	{
		alert("Please enter a valid SingTel Mobile No."); 
		f.Mobile.value='65';
		f.Mobile.focus();
		return false;
	}
	else if(isNaN(f.Mobile.value))
	{
		alert("Mobile no. should be only numbers");
		f.Mobile.value='65';
		f.Mobile.focus();
		return false;
	}
	else if (f.Mobile.value.length !=10 && f.Mobile.value.length !=8)
	{
		alert("Mobile no. should have at least 8 digits");
		f.Mobile.value='65';
		f.Mobile.focus();
		return false;
	}

	else if(f.Mobile.value=='' || f.Mobile.value.charAt(0)==' ')
	{	
		alert("Please specify your Mobile no.");
		f.Mobile.value='65';
		f.Mobile.focus();

		return false;
	}

	if (f.Mobile.value.charAt(0) == '6' && f.Mobile.value.charAt(1) == '5' && ( f.Mobile.value.charAt(2) =='8' || f.Mobile.value.charAt(2)=='9'))
	{	
		return true;
	}
	else
	{
		if ( f.Mobile.value.charAt(0)=='9' || f.Mobile.value.charAt(0)=='8' )
		{
			return true;
		}
		else
		{
			alert("Please specify a valid Mobile no.");
			f.Mobile.value='65';
			f.Mobile.focus();

			return false;		
		}
	}

	return true;
}

function smspwdCheck(f)
{
	if(isBlank(f.SMSPassword) || !f.SMSPassword.value) 
	{
		alert("Please enter the password."); 
		f.SMSPassword.value='';
		f.SMSPassword.focus();
		return false;
	}
	else if(isNaN(f.SMSPassword.value))
	{
		alert("Please enter the correct password.");
		f.SMSPassword.value='';
		f.SMSPassword.focus();
		return false;
	}

	return true;
}

function validateSignUp(doc)
{	
    var x;

    var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    var valid2 = "- ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    var ok = "yes";
    var temp;
    for (var i=0; i<doc.NewPassword.value.length; i++) 
    {
        temp = "" + doc.NewPassword.value.substring(i, i+1);
        if (valid.indexOf(temp) == "-1") 
        	ok = "no";
    }
    if (ok == "no") 
    {
        alert("Only characters and numbers are accepted");
	doc.NewPassword.value='';
	doc.NewPassword.focus();
	return false;
    }
    else if(doc.NewPassword.value=='' && doc.NewPassword.value.charAt(0)==' ')
    {
	alert("Please specify your preferred password");
	doc.NewPassword.value='';
	doc.NewPassword.focus();
	return false;

    }
    else if(doc.NewPassword.value.length<6 || doc.NewPassword.value.length>10)
    {
	alert("Please enter a password of length within 6-10 characters");
	doc.NewPassword.value='';
	doc.NewPassword.focus();
	return false;

    }

    if(doc.ConfirmPassword.value=='' || doc.ConfirmPassword.value.charAt(0)==' ')
    {
	alert("Please specify your repeat password");
	doc.ConfirmPassword.value='';
	doc.ConfirmPassword.focus();
	return false;
    }
    else if(doc.ConfirmPassword.value!=doc.NewPassword.value)
    {
	alert("Password confirmation does not match. Please re-enter password");
	doc.ConfirmPassword.value='';
	doc.NewPassword.value='';
	doc.NewPassword.focus();
	return false;
    }

    ok = "yes";
    for (var i=0; i<doc.FirstName.value.length; i++) 
    {
        temp = "" + doc.FirstName.value.substring(i, i+1);
        if (valid2.indexOf(temp) == "-1") 
        	ok = "no";
    }

    if (ok == "no") 
    {
      	alert("Only characters and numbers are accepted");
	doc.FirstName.value='';
	doc.FirstName.focus();
	return false;
    }    
    
    ok = "yes";    
    for (var i=0; i<doc.LastName.value.length; i++) 
    {
            temp = "" + doc.LastName.value.substring(i, i+1);
            if (valid2.indexOf(temp) == "-1") 
            	ok = "no";
    }
    
    if (ok == "no") 
    {
      	alert("Only characters and numbers are accepted");
	doc.LastName.value='';
	doc.LastName.focus();
	return false;
    }
    
    if(doc.FirstName.value=='' || doc.FirstName.value.charAt(0)== ' ')
    {	
    	alert("Your first name/surname is required");
	doc.FirstName.value='';
	doc.FirstName.focus();
	return false;
    } 

    if(doc.LastName.value=='' || doc.LastName.value.charAt(0)== ' ')
    {	
	alert("Your last name is required");
	doc.LastName.value='';
	doc.LastName.focus();
	return false;
    }

    if(doc.Email.value!='')
    {	
    	x=doc.Email.value;
	if(x.indexOf('@')<1 || (x.indexOf('@')==(x.length-1)))
	{	
		alert("Invalid email address");
		doc.Email.value='';
		doc.Email.focus();
	  return false;
	}
    }

    if (doc.Gender.value=='')
    {
    	alert("Please select your gender.");
    	doc.Gender.focus();
	return false;
    }

    if (doc.DOB_Year.value.length !=4)
    {
	alert("Please enter a valid birthdate");
	doc.DOB_Year.value='';
	doc.DOB_Year.focus();
	return false;	
    }
    
    if (isDate(doc.DOB_Day.value,doc.DOB_Month.value,doc.DOB_Year.value)==false)
    {
	alert("Please enter a valid birthdate");
	doc.DOB_Year.value='';
	doc.DOB_Year.focus();
	return false;	
    }
    return true;

}

function y2k(number) 
{ 
    return (number < 1000) ? number + 1900 : number; 
}

function isDate (day,month,year) {

    var today = new Date();
    year = ((!year) ? y2k(today.getYear()):year);
    month = ((!month) ? today.getMonth():month-1);
    if (!day) return false
    var test = new Date(year,month,day);
    if ( (y2k(test.getYear()) == year) &&
         (month == test.getMonth()) &&
         (day == test.getDate()) )
        return true;
    else
        return false;
}

function deleteItem() {
	document.myWAPForm.action = "wl_delete.jsp"
	document.myWAPForm.submit()
}

function promptforlogin(url)
{
	alert ('Please login before accessing the service');
	location.href = url;
}

function validatefield()
{
    var doc = document.registerForm;
    var mobilestr = "";
    mobilearray = new Array()
    var valid = " ;,0123456789"
    var ok = "yes";
    var temp;
    for (var i=0; i<doc.textfield.value.length; i++)
    {
        temp = "" + doc.textfield.value.substring(i, i+1);
        if (valid.indexOf(temp) == "-1") ok = "no";
    }

    if (ok == "no")
    {
          alert("Only semi-colons, commas and numbers are accepted");
    			doc.textfield.focus();
    			return false;
    }
    else if(doc.textfield.value.length < 8)
	  {
    		alert("This field should contain at least eight digits");
    		doc.textfield.value='9';
    		doc.textfield.focus();
    		return false;
	  }
    else if(doc.textfield.value=='' || doc.textfield.value.charAt(0)==' ')
	  {
    		alert("Please specify your recipent number.");
    		doc.textfield.value='9';
    		doc.textfield.focus();
    		return false;
	  }

    if ( doc.message.value.length < 1 )
    {
        doc.message.focus();
    	 alert("Please enter the message to be sent.");
    	 return false;
    }

    var delimiter = ";";
    i=0;
    mobilestr = doc.textfield.value;
    while (mobilestr.indexOf(delimiter)>0)
    {
        temp = mobilestr.replace(delimiter, " ");
        mobilestr=temp;
        i++;
        if ( i > 15)
        {
            alert("Please ensure only 10 numbers are entered.");
    			  doc.textfield.focus();
    			  return false;
        }
    }

    delimiter = ",";
    i=0;
    while (mobilestr.indexOf(delimiter)>0)
    {
        temp = mobilestr.replace(delimiter, " ");
        mobilestr=temp;

        i++;
        if ( i > 15)
        {
            alert("Please ensure only 10 numbers are entered.");
    			  doc.textfield.focus();
    			  return false;
        }
    }

    var arraymobilenum = mobilestr.split(" ");
    var j = 0;
    for ( i=0; i<arraymobilenum.length; i++)
    {
        if (( arraymobilenum[i].length > 7 ) && ( arraymobilenum[i].length < 11 ))
        {
            if((arraymobilenum[i].charAt(0) == '6' && arraymobilenum[i].charAt(1)=='5') || arraymobilenum[i].charAt(0)=='9' || arraymobilenum[i].charAt(0)=='6' )
            {
                mobilearray[j++]=arraymobilenum[i] ;
                if ( j > 10 )
                {
                    alert("Please ensure only 10 valid mobile numbers are entered.");
    			          doc.textfield.focus();
    			          return false;
                }
            }
        }
    }

    doc.textfield.value=mobilearray.join("|");
doc.submit();
    //return true;
}

function clearmsg()
{
	var doc = document.registerForm;
	doc.message.value='';
}

function textareaMax(myfield, myfield1, maxlimit)

{

	if (myfield.value.length > maxlimit) // if too long...trim it!

	{

		alert("Your message has exceeded the maximum length!");

		myfield.value = myfield.value.substring(0, maxlimit);

	}

	myfield1.value=myfield.value.length;

}

function goAtGlance(g)
{
var agStr1, agStr2, agStr3, selURL=g.options[g.selectedIndex].value
if(selURL&&selURL!="#"){
	agStr1=selURL.substring(0,7); agStr2=selURL.lastIndexOf("e-ideas.com"); agStr3=selURL.lastIndexOf("ideas.singtel")
	if(agStr1!="http://" || agStr2!=-1 || agStr3!=-1) {location=selURL}
	else {window.open(selURL); for(i=0;i<g.length;i++) {if(g.options[i].defaultSelected) g.selectedIndex=i}}
}}
