function createXmlHttpRequest()
{
	var http_request = null;
	if(window.XMLHttpRequest) { 
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	}
	else if (window.ActiveXObject) { 
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) { 
		return false;
	}
	return http_request;
}
function loadMovieItems(id){
	var url = "show_movie_items.php?type="+id;
	var page = document.getElementById("page").value;
	var keyword = document.getElementById("keyword_id").value;
	var attstr = document.getElementById("attstr").value;
	var ctg_id = document.getElementById("ctg_id").value;
	var ctg_ctg_id = document.getElementById("ctg_ctg_id").value;
	if(document.getElementById("check").checked){ url += "&check=y"; }
	if(page>0){ url += "&page="+page;}
	if(keyword != ""){ url += "&keyword="+keyword;}
	if(attstr != ""){ url += "&attstr="+attstr;}
	if(ctg_id != ""){ url += "&ctg_id="+ctg_id;}
	if(ctg_ctg_id != ""){ url += "&ctg_ctg_id="+ctg_ctg_id;}
	url += "&temp="+Math.random();
	var xmlHttp = createXmlHttpRequest();
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				document.getElementById("movieitems").innerHTML = xmlHttp.responseText;
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function loadCategory(ctg_id, ctg_ctg_id, ctg_name){
	document.getElementById("ctg_id").value = ctg_id;
	document.getElementById("ctg_ctg_id").value = ctg_ctg_id;
	if(loadMovieItemsModule == "index.php"){
		if(ctg_name!=''){
			document.getElementById("show_keywords").innerHTML = ctg_name;
		}
		if(ctg_id>0||ctg_ctg_id>0){
			document.getElementById("search_in").style.display = 'block';
			document.getElementById("ctg_name").innerHTML = ctg_name;
			document.getElementById("keyword_id").value = '';
		}else{
			document.getElementById("search_in").style.display = 'none';
		}
		
		document.getElementById("check").checked = true;
	}
	var type = document.getElementById("current_tab").value;
	if(type == "") type = 0;
	var url = "show_category.php?1";
	if(ctg_id != ""){ url += "&ctg_id="+ctg_id;}
	if(ctg_ctg_id != ""){ url += "&ctg_ctg_id="+ctg_ctg_id;}
	url += "&temp="+Math.random();
	var xmlHttp = createXmlHttpRequest();
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				document.getElementById("categories").innerHTML = xmlHttp.responseText;
				if(loadMovieItemsModule == "index.php")loadMovieItems(type);
				if(loadMovieItemsModule == "show.php" && ctg_id != ""){
					window.location="index.php?ctg_id="+ctg_id;
				}
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function loadMovieDetail(cmp_id){
	document.getElementById("cmp_id").value = cmp_id;
	var url = "show_movie_details.php?cmp_id=" + cmp_id;
	url += "&temp="+Math.random();
	var xmlHttp = createXmlHttpRequest();
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				loadCategory('','');
				var s = xmlHttp.responseText.split(/<!-- AJAX-EVAL/i);
				if (typeof(s[1]) != "undefined") {
					document.getElementById("movie_details").innerHTML = s[0];
					eval(s[1]);
				} else {
					document.getElementById("movie_details").innerHTML = xmlHttp.responseText;
				}
				loadMoviePromoted(cmp_id);
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}
function loadMoviePromoted(cmp_id){
	var url = "show_details_promoted.php?cmp_id="+cmp_id;
	url += "&temp="+Math.random();
	var xmlHttp = createXmlHttpRequest();
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				document.getElementById("details_promoted").innerHTML = xmlHttp.responseText;
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);		
}
function gotopage(page){
	var type = document.getElementById("current_tab").value;
	document.searchform.page.value = page;
	loadMovieItems(type);
}
function searchMovies(){
	if(loadMovieItemsModule=="show.php"){
		window.location="index.php?keyword="+document.getElementById("keyword_id").value;
	}
	var type = document.getElementById("current_tab").value;
	var keyword = document.getElementById("keyword_id").value
	if(keyword!=''){
		document.getElementById("show_keywords").innerHTML = keyword;
	}else if(document.getElementById("ctg_name").innerHTML!=''){
		document.getElementById("show_keywords").innerHTML = document.getElementById("ctg_name").innerHTML ;
	}else {
		document.getElementById("show_keywords").innerHTML = 'Featured';
	}
	document.getElementById("attstr").value = attarr;
	loadMovieItems(type);
}
function removefromarr(arr,v) {
    var a=[];
    for(i=0; i<arr.length; i++) {
        if(arr[i]!=v) {
            a = a.concat(arr[i]);
        }
    }
    return a;
}
function displayattvalue(id)
{
	if(document.getElementById('subattvalue_'+id).style.display=='none'){
		document.getElementById('subattvalue_'+id).style.display='';
		document.getElementById('attimg_'+id).src = 'images/left_c2.gif';
	}else{
		document.getElementById('subattvalue_'+id).style.display='none';
		document.getElementById('attimg_'+id).src = 'images/left_c1.gif';
	}
}

function displaySubCategory(id)
{
	if(document.getElementById('subcategory_'+id).style.display=='none'){
		document.getElementById('subcategory_'+id).style.display='';
		document.getElementById('pcatimg_'+id).src = 'images/left_c2.gif';
	}else{
		document.getElementById('subcategory_'+id).style.display='none';
		document.getElementById('pcatimg_'+id).src = 'images/left_c1.gif';
	}
}


function refreshCagtegoryMenu()
{
	var spreadidstr = getCookie("SPREAD_ID_STR");
	if(spreadidstr==undefined){
		spreadidstr = '';
	}
	var spreadidarr = spreadidstr.split(",");
	for(i=0;i<spreadidarr.length;i++)
	{
		if(spreadidarr[i]!='')
		{
			document.getElementById('subcategory_'+spreadidarr[i]).style.display='';
			document.getElementById('pcatimg_'+spreadidarr[i]).src = 'images/left_c1.gif';
		}
	}
}
function getCookieVal(offset)
{
	var endstr = document.cookie.indexOf(";", offset);
	if(endstr == -1)
	{
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
}

// primary function to retrieve cookie by name
function getCookie(name)
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while(i < clen)
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		{
			return getCookieVal(j);
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if(i == 0) break;
	}
	return;
}

// store cookie value with optional details as needed
function setCookie(name, value, expires, path, domain, secure)
{
	document.cookie = name + "=" + escape(value) +
	((expires) ? "; expires=" + expires : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

// remove the cookie by setting ancient expiration date
function deleteCookie(name,path,domain)
{
	if(getCookie(name))
	{
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}
function setAttvalue(id,a_id)
{
	attarr.push(a_id+'_'+id);
	extarr = removefromarr(extarr,a_id);
	if(attarr.length>0){
		document.getElementById("setattbox").style.display = '';
	}
	document.getElementById("subatt_"+id).style.display = '';
	document.getElementById("att_"+a_id).style.display = 'none';
	document.getElementById("subattvalue_"+a_id).style.display = 'none';
	if(extarr.length==0){
		document.getElementById("extattbox").style.display = 'none';
	}
	searchMovies();
}

function removeAttvalue(id,a_id)
{
	var tmp = a_id+'_'+id;
	attarr = removefromarr(attarr,tmp);
	extarr.push(a_id);
	if(extarr.length>0){
		document.getElementById("extattbox").style.display = '';
	}
	document.getElementById("subatt_"+id).style.display = 'none';
	document.getElementById("att_"+a_id).style.display = 'block';
	if(attarr.length==0){
		document.getElementById("setattbox").style.display = 'none';
	}
	document.getElementById('attimg_'+a_id).src = 'images/left_c1.gif';
	searchMovies();
}

function setattvalueTovalue()
{
	var str = '';
	for(i=0;i<attarr.length;i++){
		str = str + attarr[i]+'|';
	}
	document.searchform.attstr.value = str;
	document.searchform.submit();
}
function changeCtg() {
	if(document.getElementById('ctg').style.display=='none') {
		document.getElementById('ctg').style.display='block';
		document.getElementById('ctg_img').src='images/template_50.gif'
	} else {
		document.getElementById('ctg').style.display='none';
		document.getElementById('ctg_img').src='images/template_50.gif'
	}
}
function changeCtgCtg() {
	if(document.getElementById('ctg_ctg').style.display=='none') {
		document.getElementById('ctg_ctg').style.display='block';
		document.getElementById('ctg_ctg_img').src='images/template_50.gif'
	} else {
		document.getElementById('ctg_ctg').style.display='none';
		document.getElementById('ctg_ctg_img').src='images/template_50.gif'
	}
}
function showsub_a(id){
	for (i=0;i<4;i++){
		document.getElementById("tab_a"+i).className = "tab_link_center";
		document.getElementById("tab_left_a"+i).className = "tab_link_left";
		document.getElementById("tab_right_a"+i).className = "tab_link_right";
	}
	document.getElementById("tab_a"+id).className = "tab_active_center";
	document.getElementById("tab_left_a"+id).className = "tab_active_left";
	document.getElementById("tab_right_a"+id).className = "tab_active_right";
	document.getElementById("current_tab").value = id;
	document.getElementById("page").value = 0;
	loadMovieItems(id);
}

function showsub_c(id){
	if(!isLogin){
		alert('Please login first');
		return;
	}
	for (i=0;i<3;i++){
		document.getElementById("tab_a"+i).className = "tab_link_center";
		document.getElementById("tab_left_a"+i).className = "tab_link_left";
		document.getElementById("tab_right_a"+i).className = "tab_link_right";
	}
	document.getElementById("tab_a"+id).className = "tab_active_center";
	document.getElementById("tab_left_a"+id).className = "tab_active_left";
	document.getElementById("tab_right_a"+id).className = "tab_active_right";
	//document.getElementById("current_tab").value = id;
	//document.getElementById("page").value = 0;
	//loadMovieItems(id);
	if(id==0){
		loadCampaignOverview();
	}else if(id==1){
		newCampaign();
	}else if(id==2){
		accountDetail();
	}else if(id==3){
		location.href='logout.php';
	}
}

show_company_form = null;
function ontop() {
	if (show_company_form && !show_company_form.closed) {
		show_company_form.focus()
	}
		
}

function openCompany(id)
{
	str="companyAddress.php?cpn_id="+id;
	show_company_form = window.open(str,"detailwin","height=280,width=360,left=40,top=80");
}
function defaultvote(default_rate)
{
	for(var i=1;i<=5;i++){
		if(i<=default_rate){
			document.getElementById('star'+i).src = "images/star_f.gif";
		}else{
			document.getElementById('star'+i).src = "images/star_e.gif";
		}
	}
}

function changestar(id){
	cmp_id = document.getElementById("cmp_id").value
	if(cmp_str.indexOf(','+cmp_id+',')>-1){
		return false;
	}
	for(var i=1;i<=5;i++){
		if(i<=id){
			document.getElementById('star'+i).src = "images/star_f.gif";
		}else{
			document.getElementById('star'+i).src = "images/star_e.gif";
		}
	}
}


function vote(score)
{
	if(score<1||score>5){
		return false;
	}
	if(cmp_str!=""){
		if(cmp_str.indexOf(','+cmp_id+',')>-1){
			return false;
		}
	}else{
		cmp_str = ",";
	}

	var url = "vote.php?cmp_id="+cmp_id+"&score="+score+"&temp="+Math.random();
	var xmlHttp = createXmlHttpRequest();
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				var arr = xmlHttp.responseText.split("|");
				default_rate = arr[0];
				defaultvote(score);
				document.getElementById("rates_num").innerHTML = arr[1];
				for(var i=1;i<=5;i++){
					document.getElementById('star'+i).onClick = function(){};
					document.getElementById('star'+i).onmouseover = function(){};
				}
				document.getElementById('rating_star').onmouseout = function(){};
				cmp_str += cmp_id+",";
				setCookie("cmp_str", cmp_str);
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function login()
{
	if(!validate_email(document.LogForm.EMAIL.value)){
		alert('E-mail error!');
		return false;
	}
	if(document.LogForm.PASSWORD.value==''){
		alert('Please enter password');
		return false;
	}
	var url = "login.php?email="+document.LogForm.EMAIL.value+"&pwd="+document.LogForm.PASSWORD.value+"&temp="+Math.random();
	var xmlHttp = createXmlHttpRequest();
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				if(xmlHttp.responseText!="0"){	
					document.getElementById("CPN_CPN_ID").value = xmlHttp.responseText;
					isLogin = true;
					loadCampaignOverview();
				}else{
					alert('Your can not login on!');
				}
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function validate_email(field)
{
	if(field==""){
		return false;
	}
	apos=field.indexOf("@");
	dotpos=field.lastIndexOf(".");
	if(apos<1 || dotpos-apos<2) 
		return false; 
	else
		return true;
}

function Register(n)
{
	if(n.NAME.value==''){
		alert('Please enter company name');
		return false;
	}

	if(!validate_email(n.EMAIL.value)){
		alert('Email error!');
		return false;
	}

	if(n.PASSWORD.value==''||n.PASSWORD.value!=n.REPASSWORD.value){
		alert('Please check your password');
		n.PASSWORD.value = '';
		n.REPASSWORD.value = '';
		return false;
	}

	if(n.CITY.value==''){
		alert('Please enter city');
		return false;
	}
	
	var url = "CampaignCommand.php?cmd=checkCpnEmail&email="+n.EMAIL.value+"&temp="+Math.random();
	var xmlHttp = createXmlHttpRequest();
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				if(xmlHttp.responseText=="0"){
					n.submit();
				}else{
					alert('The emial address has existed!');
					return false;
				}
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function editAccount(n){
	if(n.NAME.value==''){
		alert('Please enter company name');
		return false;
	}

	if(!validate_email(n.EMAIL.value)){
		alert('Email error!');
		return false;
	}

	if(n.CITY.value==''){
		alert('Please enter city');
		return false;
	}
	n.submit();
}

function loadCampaignOverview()
{
	var CPN_ID = document.getElementById("CPN_CPN_ID").value;
	var ORDERING = document.getElementById("ORDERING").value;
	var ORDERBY = document.getElementById("ORDERBY").value;
	var page = document.getElementById("page").value;
	var url    = "campaignsoverview.php?cpn_id="+CPN_ID+"&orderby="+ORDERBY+"&ordering="+ORDERING+"&page="+page+"&temp="+Math.random();
	var xmlHttp = createXmlHttpRequest();
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				document.getElementById("account_content").innerHTML = xmlHttp.responseText;
				reloadDivSize("compaignoverview");
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function changeStatus(cmp_id,status)
{
	var url = "CampaignCommand.php?cmd=changeStatus&cmp_id="+cmp_id+"&status="+status+"&temp="+Math.random();
	var xmlHttp = createXmlHttpRequest();
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				loadCampaignOverview();
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function removeCampaign(cmp_id)
{
	if(!confirm('Are you sure you want to remove this?')){
		return false;
	}
	var url = "CampaignCommand.php?cmd=remove&cmp_id="+cmp_id+"&temp="+Math.random();
	var xmlHttp = createXmlHttpRequest();
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				loadCampaignOverview();
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function newCampaign()
{
	var url = "CampaignCommand.php?cmd=new&temp="+Math.random();
	var xmlHttp = createXmlHttpRequest();
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				document.getElementById("account_content").innerHTML = xmlHttp.responseText;
				reloadDivSize("campaignarea");
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function submitCampaign(n)
{
	if(n.action.value=='editCampaign'){
		if(!confirm('When you save the campaign returns to status waiting for approval and wil therefore not be displayed on the website till approved, are you sure you want to proceed?')){
			return false;
		}
	}
	if(n.TITLE.value==''){
		alert('Please enter campaign title');
		return false;
	}
	if (n.CTG_CTG_ID.value == "") {
		alert('Please select a category');
		return false;
	}
	if (n.KEYWORDS.value == "") {
		alert('Please enter keywords');
		return false;
	}
	n.submit();
}

function editCampaign(cmp_id)
{
	var url = "CampaignCommand.php?cmd=edit&cmp_id="+cmp_id+"&temp="+Math.random();
	var xmlHttp = createXmlHttpRequest();
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				var s = xmlHttp.responseText.split(/<!-- AJAX-EVAL/i);
				if (typeof(s[1]) != "undefined") {
					document.getElementById("account_content").innerHTML = s[0];
					eval(s[1]);
					reloadDivSize("campaignarea");
				} else {
					document.getElementById("account_content").innerHTML = xmlHttp.responseText;
					reloadDivSize("campaignarea");
				}
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}


function accountDetail(){
	var url = "CampaignCommand.php?cmd=accountDetail&temp="+Math.random();
	var xmlHttp = createXmlHttpRequest();
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				document.getElementById("account_content").innerHTML = xmlHttp.responseText;
				reloadDivSize("registerarea");
			}
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function changeorder(field){
	var ORDERING = document.getElementById("ORDERING").value;
	if(document.getElementById("ORDERBY").value==field){
		if(document.getElementById("ORDERING").value=='ASC'){
			document.getElementById("ORDERING").value='DESC';
		}else{
			document.getElementById("ORDERING").value='ASC';
		}
	}else{
		document.getElementById("ORDERING").value='ASC';
	}
	document.getElementById("ORDERBY").value=field;
	loadCampaignOverview();
}

function getpassword(n){
	if(!validate_email(n.EMAIL.value)){
		alert('Email error!');
		return false;
	}
	n.submit();
}

function reloadDivSize(div_id)
{
	var content_height = document.getElementById(div_id).offsetHeight;
	content_height = content_height<=400 ? 400 : (content_height+20);
	document.getElementById("account_content").style.height = content_height+"px";
	
}

function checkUploadFile(name) {
	//var types = ["mpeg","mpg","avi","wmv","mov","asx","asf","mp4","xvid","flv","swf"];
	var types = ["mpeg","mpg","avi","wmv","mov","asx","asf","mp4","xvid","flv"];
	type = name.value.substr(name.value.lastIndexOf('.')+1).toLowerCase();
	for(var i=0; i<types.length; i++) {
		typestr += types[i]+" ";
		if(type == types[i]) {
			if(type == "swf"){
				if(confirm('The swf cannot display fix the size of the player,do you want to continue ?')){
					return ;
				}else{
					name.value = '';
					return ;
				}
			}else{
				return;
			}
		}
	}
	alert('update files type must ' + typestr);
	name.value = '';
}

function clickLogin(e) {
	e = e||event;
	if(e.keyCode==13 && document.getElementById('changeKey').value == 1) {
		document.getElementById('changeKey').value = 0;
		login();
	} else if(e.keyCode!=13) {
		document.getElementById('changeKey').value = 1;
	}
}
